package com.tich.program.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.tich.constant.ExceptionConstant;
import com.tich.constant.RabbitMqConstant;
import com.tich.exception.TicketCategoryNotFoundException;
import com.tich.program.domain.dto.TicketCategoryDeleteDTO;
import com.tich.program.domain.dto.TicketCategorySelectDTO;
import com.tich.program.domain.entity.TicketCategory;
import com.tich.program.domain.vo.TicketCategoryVO;
import com.tich.program.mapper.TicketCategoryMapper;
import com.tich.program.service.ITicketCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 票分类表 服务实现类
 * </p>
 *
 * @author 
 * @since 2025-05-14
 */
@Service
public class TicketCategoryServiceImpl extends ServiceImpl<TicketCategoryMapper, TicketCategory> implements ITicketCategoryService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 根据节目类型id查询门票类型
     * @param ticketCategorySelectDTO
     * @return
     * */
    public List<TicketCategoryVO> queryTicketCategoryByIds(TicketCategorySelectDTO ticketCategorySelectDTO) {
        List<TicketCategory> list = lambdaQuery()
                .in(TicketCategory::getProgramCategoryId, ticketCategorySelectDTO.getIds())
                .orderByAsc(TicketCategory::getId)
                .list();
        return BeanUtil.copyToList(list, TicketCategoryVO.class);
    }

    /**
     * 根据ids批量删除门票类型
     * @param ticketCategoryDeleteDTO
     * */
    public void deleteTicketCategorys(TicketCategoryDeleteDTO ticketCategoryDeleteDTO) {
        //1.查询门票类型是否存在
        List<TicketCategory> ticketCategories = lambdaQuery().in(TicketCategory::getId, ticketCategoryDeleteDTO.getIds()).list();
        if(ticketCategories.isEmpty()){
            throw new TicketCategoryNotFoundException(ExceptionConstant.TICKET_CATEGORY_NOT_FOUND);
        }
        //2.删除门票类型
        removeByIds(ticketCategoryDeleteDTO.getIds());
        //3.将消息发送至交换机
        rabbitTemplate.convertAndSend(RabbitMqConstant.PROGRAM_DIRECT,
                RabbitMqConstant.TICKET_CATEGORY_DELETE_KEY,
                ticketCategories);
    }
}
